import threading
import time


class MyThread1(threading.Thread):
    def run(self):
        mutexA.acquire()

        # 上锁后延时一秒，等待另外一个线程把mutexB上锁
        print(self.name+'----do1--up')
        time.sleep(1)

        # 此时会堵塞，因为mutexB已经被另外的线程先上锁了
        mutexB.acquire()
        print(self.name+'----do1--down')
        mutexB.release()

        # 解锁mutexA

        mutexA.release()

class MyThread2(threading.Thread):
    def run(self):
        mutexB.acquire()

        # 上锁后延时一秒，等待另外一个线程把mutexA上锁
        print(self.name+'----do1--up')
        time.sleep(1)

        # 此时会堵塞，因为mutexA已经被另外的线程先上锁了
        mutexA.acquire()
        print(self.name+'----do1--down')
        mutexA.release()

        # 解锁mutexB

        mutexB.release()

mutexA = threading.Lock()
mutexB = threading.Lock()

if __name__ == '__main__':
    t1=MyThread1()
    t2=MyThread2()
    t1.start()
    t2.start()
    
